/*
 * @Copyright: Copyright (c) 2030 吴周海
 * @Author: wzh200x@126.com
 * @Date: 2025-10-09 16:06:00
 * @LastEditors: wzh200x@126.com
 * @LastEditTime: 2025-10-13 10:28:27
 * @FilePath: \gogamewebserver\goGameWebServer\internal\model\entity\accountModel.go
 * @Description: 文件功能描述
 */
package entity

import (
	"goGameWebServer/internal/common/logger"
	"goGameWebServer/internal/common/utils"
	"goGameWebServer/internal/defines/cerror"
	"goGameWebServer/internal/defines/dataType"
	"goGameWebServer/internal/interfaces"
	"goGameWebServer/internal/manager/databasemanager"

	"github.com/gogf/gf/v2/errors/gcode"
	"github.com/gogf/gf/v2/errors/gerror"
	"github.com/gogf/gf/v2/util/gmeta"
)

type AccountModelData struct {
	gmeta.Meta           `orm:"table: account"`
	Id                   dataType.TypeDataBaseId `orm:"id"`                   // 账号ID
	AccountName          string                  `orm:"accountName"`          // 账号
	Password             string                  `orm:"password"`             // 密码
	ThirdPlatformType    int                     `orm:"thirdPlatformType"`    // 平台类型
	ThirdPlatform        string                  `orm:"thirdPlatform"`        // 平台名称
	ThirdAccount         string                  `orm:"thirdAccount"`         // 平台账号
	ThirdToken           string                  `orm:"thitdToken"`           // 平台token
	ThirdTokenExpireTime int64                   `orm:"thirdTokenExpireTime"` // token过期时间
	AvatarId             dataType.TypeDataBaseId `orm:"avatarId"`             // 角色ID
	LoginTime            string                  `orm:"loginTime"`            // 登录时间
	CreateTime           string                  `orm:"createTime"`           // 创建时间
	LogoutTime           string                  `orm:"logoutTime"`           // 登出时间
}

func (accountModelData *AccountModelData) Reset() {
	accountModelData.Id = 0
	accountModelData.AccountName = ""
	accountModelData.Password = ""
	accountModelData.AvatarId = 0
	accountModelData.LoginTime = ""
	accountModelData.CreateTime = ""
	accountModelData.LogoutTime = ""
}

type AccountModel struct {
	EntityModel
}

func (accountModel *AccountModel) LoadEntityDataFromDB(records interface{}) error {
	base, err := databasemanager.ManagerDatabase().DefaultDataBase()
	if err != nil {
		return err
	}
	if entityData, ok := accountModel.EntityData.(*AccountModelData); ok {
		one, err := base.GetDb().Model(accountModel.Template).Where("accountName", entityData.AccountName).One()
		if err != nil {
			return err
		}
		if one == nil {
			return accountModel.Create()
		}
		_ = one.Struct(accountModel.EntityData)
		accountModel.SetEntityDBID(entityData.Id)
		return nil
	}
	return gerror.NewCodef(gcode.New(cerror.ERROR_ENTITY_PROPERTY_INVALID, "", nil),
		"avatar module model data is invalid")
}

func (accountModel *AccountModel) Create() error {
	if entityData, ok := accountModel.EntityData.(*AccountModelData); ok {
		entityData.CreateTime = utils.GetNowTimeFormat()
		entityData.LoginTime = utils.GetNowTimeFormat()
		err := accountModel.CreateDataToDB()
		entityData.Id = accountModel.EntityDBID
		if err != nil {
			return err
		}
		logger.LoggerIns().Debug("create account base success")
		return nil
	}
	return gerror.NewCodef(gcode.New(cerror.ERROR_ENTITY_PROPERTY_INVALID, "", nil),
		"avatar module model create fail, data is invalid")
}

func NewAccountModel(name string) interfaces.IEntityModel {
	m := &AccountModel{
		EntityModel: NewEntityModel(name),
	}
	m.SetEntityData(&AccountModelData{
		Id:          0,
		AccountName: "",
		AvatarId:    0,
		LoginTime:   "",
		CreateTime:  "",
	})
	m.SetTemplate(m.EntityData)
	return m
}
